//
//  EntrustViewController.m
//  RedManStock
//
//  Created by lanou3g on 15/8/2.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "EntrustViewController.h"
#import "EntrustTableViewCell.h"
#import "MyCoreEntrust.h"
#import "MyCoreHandle.h"
#import "UIDefine.h"
@interface EntrustViewController ()
@property (nonatomic,retain) NSArray *entrustArray;
@property (nonatomic,retain) NSMutableArray *buyEntrustArray;
@property (nonatomic,retain) NSMutableArray *sellEntrustArray;

@end

@implementation EntrustViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.entrustArray=[NSArray array];
    self.buyEntrustArray=[NSMutableArray array];
    self.sellEntrustArray=[NSMutableArray array];
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"EntrustTableViewCell" bundle:nil] forCellReuseIdentifier:@"EntrustTableViewCell"];
    
}

//实现刷新的功能
- (void)loadNewData{
    self.entrustArray=[[MyCoreHandle shareHandle] selectAllEntrust];
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [self loadNewData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    int i=0;
    if (self.buyEntrustArray.count>0) {
        i++;
    }
    if (self.sellEntrustArray.count>0) {
        i++;
    }
    return i;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    if (self.buyEntrustArray.count>0&&self.sellEntrustArray.count>0) {
        if (section==0) {
            return @"买入委托";
        }
        else return @"卖出委托";
    }
    else if(self.buyEntrustArray.count>0 && self.sellEntrustArray.count==0){
        return @"买入委托";
    }
    else if(self.buyEntrustArray.count==0 && self.sellEntrustArray.count>0){
        return @"买入委托";
    }
    else return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.entrustArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EntrustTableViewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:@"EntrustTableViewCell" forIndexPath:indexPath];
    
    MyCoreEntrust *entrust=nil;
    if (indexPath.section==0 && self.buyEntrustArray>0) {
        entrust=self.buyEntrustArray[indexPath.row];
    }
    else if (indexPath.section==0 && self.buyEntrustArray==0){
        entrust=self.sellEntrustArray[indexPath.row];
    }
    else if (indexPath.section==1){
        entrust=self.sellEntrustArray[indexPath.row];
    }
    else{
        
    }
    cell.entrust=entrust;
    cell.tableView=tableView;
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
